package com.igirl.manage.controller;


import com.github.pagehelper.PageInfo;
import com.igirl.common.bean.EasyUiResult;
import com.igirl.manage.pojo.Content;
import com.igirl.manage.service.content.ContentService;
import java.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @Author: jiaheng
 * @Description:
 * @Date: Created in 2017/12/31
 * @Modified:
 */
@Controller
@RequestMapping("content")
public class ContentController {

    @Autowired
    private ContentService contentService;

    /**
     * 新增内容
     */
    @RequestMapping(method=RequestMethod.POST)
    public ResponseEntity<Void> addContent(Content content){

        try {
            Boolean flag = this.contentService.saveContent(content);
            if(flag){
                return ResponseEntity.status(HttpStatus.CREATED).build();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 根据内容分类查询内容
     * 异步
     * 参数 categoryId=40&page=1&rows=20
     * 结果 EasyUiResult
     */
    @RequestMapping(method= RequestMethod.GET)
    public ResponseEntity<EasyUiResult> queryContentPageListByCategoryId(
            @RequestParam("categoryId") Long categoryId,
            @RequestParam(value="page",defaultValue="1") Integer pageNum,
            @RequestParam(value="rows",defaultValue="10") Integer pageSize){

        try {
            Content record = new Content();
            record.setCategoryId(categoryId);
            PageInfo<Content> pageInfo = this.contentService.queryPageListByWhere(record,pageNum, pageSize);
            EasyUiResult result = new EasyUiResult(pageInfo.getTotal(), pageInfo.getList());
            return ResponseEntity.status(HttpStatus.OK).body(result);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }
    
    @RequestMapping(value = "edit",method = RequestMethod.POST)
    public ResponseEntity<Content> updateContent(Content content) {
        try {
            if (content == null) {
                return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null);
            }
            this.contentService.updateSelective(content);
            return ResponseEntity.status(HttpStatus.OK).body(content);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }

    @RequestMapping(value = "delete",method = RequestMethod.POST)
    public ResponseEntity<Content> deleteContent(String[] ids) {
        try {
            if (ids == null) {
                return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null);
            }
            List list = new ArrayList();
            for (String  id: ids) {
                list.add(id);
            }
            this.contentService.deleteByIds("id",list);
            return ResponseEntity.status(HttpStatus.OK).body(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }
}
